faction_throttle_discontent = {
    every_faction = {
        limit = { exists = yes }

        # Disband factions with extremely high discontent (proxy for unnamed/bugged factions)
        if = {
            limit = { discontent >= 100 }
            disband_faction = yes  # Disband to prevent bugged factions like the unnamed "?" faction
        }
        else = {
            # Ensure discontent is reset to 0 to prevent persistent high discontent
            set_discontent = 0

            if = {
                limit = {
                    OR = {
                        is_at_war = yes  # Always update factions during war
                        global_variable = {
                            name = miv_compat_ai_logic
                            value = miv_enhanced
                        }  # Full updates in MIV enhanced mode
                    }
                }
                update_discontent = yes  # Normal faction discontent updates
            }
            else = {
                # Throttle faction updates in performance mode during peacetime
                random = {
                    chance = 50  # 50% chance to update discontent each pulse
                    update_discontent = yes
                }
            }
        }
    }
}

fix_unnamed_factions = {
    every_faction = {
        limit = {
            exists = yes
            AND = {
                num_of_members <= 5                        # Small factions are more likely to be bugged
                discontent >= 50                           # High discontent often correlates with bugged factions
                NOT = { has_faction_flag = miv_key_faction }  # Exclude MIV-specific factions
                OR = {
                    NOT = { has_name = yes }               # Unnamed (shows as '?')
                    discontent >= 100                      # Or totally maxed out factions
                }
            }
        }

        # Debug log (optional, helps track what gets disbanded)
        log = "Disbanding likely undefined or bugged faction with high discontent."

        disband_faction = yes
    }
}


Suggested Fixes:
[Crusader Kings 3 Mod Fixes]

This document outlines the necessary fixes for your Crusader Kings 3 mod, addressing both syntax errors in the event files and missing localization entries.

[I. Event File Syntax Errors]

CK3 event files are written in a specific syntax. Here's how to diagnose and correct common errors:

1.  File Location: Event files should reside in the `/events/` directory of your mod. In your case, these would be in `CK3 Performance+More Interactive Vassals Compatch/events/`.

2.  Encoding: Ensure all `.txt` files (including events) are encoded in UTF-8, *without* a Byte Order Mark (BOM). Many text editors add a BOM by default, which CK3's engine cannot properly parse and will cause the file to be skipped. Use a text editor like Notepad++ and select "Encode in UTF-8 without BOM".

3.  Namespaces: Each event file *must* have a namespace defined at the beginning:

    ```
    namespace = your_mod_namespace
    ```

    Ensure that the event ID starts with the namespace.

4.  Event Structure: Events generally follow this structure:

    ```
    character_event = {
        id = your_mod_namespace.1  # Unique ID for the event
        title = your_mod_namespace.1.title #Localization Key
        desc = your_mod_namespace.1.desc #Localization Key
        picture = GFX_event_example #Optional: Path to a gfx file
        mean_time_to_happen = {
            months = 6
        }

        trigger = {
            # Conditions for the event to trigger
            always = yes
        }

        immediate = {
            #Effects that happen the moment the event is triggered.
        }

        option = {
            name = your_mod_namespace.1.a #Localization Key
            #Effects that happen when this option is selected.
        }
    }
    ```

5.  Common Syntax Errors and Fixes:

    *   Missing or Mismatched Braces: Count your opening `{` and closing `}` braces to ensure they match. Pay attention to scope - `if` blocks, `limit` blocks, `immediate` blocks, etc. all need proper bracing.
    *   Incorrect Trigger/Effect Syntax: Crusader Kings 3 has specific triggers and effects. Check the CK3 wiki or game files for correct syntax and parameter usage.
    *   Incorrect use of = vs. ==: Assignment (=) is used to *set* a value. Comparison (==) is used in triggers to *check* if a value is equal to something. Confusing these will break your code.
    *   Missing or Incorrect Quotes: Strings must be enclosed in double quotes. Use of single quotes will result in the game failing to load the event.
    *   Missing or Incorrect Event IDs: The event IDs need to be in the correct format. Check to see whether it contains the namespace as a prefix.
    *   Incomplete on_action events block: Make sure the on_war_declared event block is completed with its appropriate definition, trigger, immediate, and options.


[II. Missing Localization Entries]

Localization ensures that the text displayed in your events is user-readable. Here's how to add localization:

1.  File Location: Localization files are `.yml` files located in `/common/localization/english/` (or the appropriate language folder). In this case, `CK3 Performance+More Interactive Vassals Compatch/common/localization/english/performance_patch_l_english.yml` is present, but potentially empty. Create new files in that location if more files are needed.

2.  File Format: Localization files follow a YAML format:

    ```yaml
    l_english:
     your_mod_namespace.1.title: "Event Title"
     your_mod_namespace.1.desc: "Event Description."
     your_mod_namespace.1.a: "Option A"
    ```

    *   `l_english:` indicates the language.  Use `l_french:`, `l_german:` etc. for other languages.
    *   Each entry consists of a localization key (matching the `title`, `desc`, `name` fields in your event files) and the text to be displayed, enclosed in double quotes.
    *   The colon `:` *must* be followed by a space.
    *   YAML is indentation-sensitive. Ensure correct indentation to avoid errors. Suggested number of spaces for identation: 2.

3.  Adding Missing Entries:

    *   Go through each event file (`performance_character_culling.txt`, `performance_patch_events.txt`, and any MIV events mentioned in `00_on_actions.txt`).
    *   For every `title`, `desc`, and `name` field in your events, ensure there's a corresponding entry in your localization files.

4. Example Fixes for missing Localization in performance_patch_l_english.yml:

Since the provided `performance_patch_l_english.yml` is empty, here's example localization for the events:

```yaml
﻿l_english:
 perf_cull.1.title: "Character Culled"
 perf_cull.1.desc: "This character has been removed to improve game performance."

 perf_patch.1.title: "Performance Patch Applied"
 perf_patch.1.desc: "Various performance tweaks have been applied."
```


[III. Additional Notes]

*   Always validate your mod by running Crusader Kings 3 in debug mode (`-debug` launch option). This will output errors to the `game.log` file in your `Crusader Kings III/` directory. Review these errors to diagnose problems more effectively.
*   Pay close attention to the scoping and bracing. This is the source of most syntax errors. Using a code editor with brace matching helps a lot.
*   Test events frequently after making changes to catch errors early. Trigger them through console commands or by designing them to fire under common conditions.
*   When integrating with other mods, thoroughly test the compatibility to ensure both mods work as intended.
